﻿# -*- coding:utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.jsonUtils_v2.table2Json_Chen import Table2Json


class ViewAreaDao(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections_v2()

    def findInfo(self):
        """
        查询 view_area
        :return:
        """
        sql = "select * from view_area"
        cur = self.conn.cursor()
        rs = cur.execute(sql)
        info = None
        if rs:
            list0 = cur.fetchall()
            info = Table2Json.view_area_list_to_json(list0)
        cur.close()
        self.conn.close()
        return info

    def findByCondition(self, **kwargs):
        """
        根据条件返回省市县列表
        :param kwargs:
        :return:
        """
        provinceId = kwargs['province_id']
        cityId = kwargs['city_id']
        districtId = kwargs['district_id']

        cur = self.conn.cursor()
        if not provinceId:
            sql = "select DISTINCT province_id, province_name from view_area"
        elif not cityId:
            sql = "select DISTINCT city_id, city_name from view_area WHERE province_id=%s" % provinceId
        elif not districtId:
            sql = "select DISTINCT district_id, district_name from view_area WHERE city_id=%s" % cityId
        else:
            sql = "select * from view_area WHERE district_id=%s" % districtId

        rs = cur.execute(sql)
        list0 = cur.fetchall()
        cur.close()
        self.conn.close()
        jsonData = Table2Json.view_area_list_to_json(list0)
        return jsonData

